package com.test.demo.activity;

import android.content.Intent;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.test.demo.PlaySurfaceView;
import com.test.demo.R;
import com.test.demo.base.BaseActivity;
import com.test.demo.base.Constants;
import com.test.demo.utils.ScreenUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 全屏预览
 */
public class FullScreenActivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_title_bar)
    RelativeLayout rlTitle;
    @BindView(R.id.surface_view_full)
    SurfaceView surfaceViewFull;
    private int logId;
    private int chan;
    private PlaySurfaceView playSurfaceView;

    @Override
    public int getContentView() {
        return R.layout.activity_full_screen;
    }

    @Override
    protected void init() {
        Intent intent = getIntent();
        if (intent == null) {
            finish();
            return;
        }
        logId = intent.getIntExtra(Constants.EXTRA.EXTRA_LOGID, -1);
        chan = intent.getIntExtra(Constants.EXTRA.EXTRA_CHAN, -1);
        String title = intent.getStringExtra(Constants.EXTRA.EXTRA_TITLE);
        if (logId == -1 || chan == -1) {
            finish();
            return;
        }
        tvTitle.setText(title);

        new Handler().post(new Runnable() {
            @Override
            public void run() {
                DisplayMetrics metric = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(metric);
                int heightPixels = metric.heightPixels - ScreenUtils.dip2px(mContext, 50);
                int widthPixels = (heightPixels * 4) / 3;
                playSurfaceView = new PlaySurfaceView(mContext);
                playSurfaceView.setFullParam(widthPixels * 2);
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                        FrameLayout.LayoutParams.MATCH_PARENT,
                        FrameLayout.LayoutParams.WRAP_CONTENT);
                params.leftMargin = (metric.widthPixels - widthPixels) / 2;
                params.topMargin = ScreenUtils.dip2px(mContext, 50);
                addContentView(playSurfaceView, params);
            }
        });
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                playSurfaceView.startPreview(logId, chan);
            }
        }, 2000);
    }

    @Override
    protected void onPause() {
        super.onPause();
        playSurfaceView.stopPreview();
    }
}
